<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addCateShowDialogBtnFn"
          >添加分类</el-button
        >
      </div>
      <el-table :data="cateList" style="width: 100%" border stripe>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="scoped">
            <el-button
              type="primary"
              size="mini"
              @click="updateCateBtnFn(scoped.row)"
              >修改</el-button
            >
            <el-button type="danger" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      @close="dialogCloseFn"
    >
      <el-form
        :model="addForm"
        ref="addRef"
        :rules="addRules"
        label-width="80px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="cancelFn">取消</el-button>
        <el-button size="mini" type="primary" @click="confirmFn">{{
          isEdit === true ? "修改" : "添加"
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getArtCateListApi, saveArtCateAPI, updateArtCateAPI } from '@/api'
export default {
  name: 'ArtCate',
  data () {
    return {
      cateList: [],
      dialogVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      isEdit: false, // false添加 true修改
      editId: '',
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名必须是1-10位的非空字符',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.getArtCateFn()
  },
  methods: {
    // 获取文章分类
    async getArtCateFn () {
      const res = await getArtCateListApi()
      console.log(res)
      this.cateList = res.data.data
    },
    // 添加分类 点击它是为了新增
    addCateShowDialogBtnFn () {
      this.dialogVisible = true
      this.isEdit = false
      this.editId = ''
    },
    // 对话框取消
    cancelFn () {
      this.dialogVisible = false
    },
    // 对话框确认添加
    confirmFn () {
      this.$refs.addRef.validate(async (valid) => {
        if (valid) {
          if (this.isEdit) {
            this.addForm.id = this.editId
            const { data: res } = await updateArtCateAPI(this.addForm)
            if (res.code !== 0) return this.$message.error(res.message)
            this.$message.success(res.message)
          } else {
            const { data: res } = await saveArtCateAPI(this.addForm)
            if (res.code !== 0) {
              return this.$message.error(res.message)
            }
            this.$message.success(res.message)
          }
          this.getArtCateFn()
          this.dialogVisible = false
        } else {
          return false
        }
      })
    },
    // 对话框关闭重置表单
    dialogCloseFn () {
      this.$refs.addRef.resetFields()
    },
    // 修改按钮，对话框内为修改
    updateCateBtnFn (obj) {
      this.dialogVisible = true
      this.addForm.cate_name = obj.cate_name
      this.addForm.cate_alias = obj.cate_alias
      this.isEdit = true
      this.editId = obj.id
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
